<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	
    var $helpers = array ('Form', 'Html','Acl','Session','Permission');
	var $components = array ('Auth', 'Acl', 'Session');
	
	const APP_DESCR = "Wonder School";
	const APP_VERSIONE = "1.0";
	
	var $utente = null;
	
	static $flags=array("ita"=>"famfamfam-flag-it","eng"=>"famfamfam-flag-gb");
		
	var $layoutPath = "bootstrap"; //per cambiare la cartella dove trovare il layout
	// var $layout = "default"; // commento perche si chiama nello stesso modo di quello asegnato sul controler
	
    function beforeFilter() {
        // This callback is executed before the action

        // disabilitare i warning
        Configure::write('debug',2);
        
		
		// print_r(LOG_INFO);die;
		// print_r(CakeLog::defaultLevels());die;
		
        // per cambiare i campi username e password di defualt utilizzati dal componente
        //$this->Auth->fields = array('username' => 'email', 'password' => 'password');

        // per specificare condizioni agli oggetti di autenticazione
        $this->Auth->authenticate = array(
 		'all' => array(
 			// 'userModel' => 'Users.User',
 			'scope' => array('User.stato' => "A")
	 		)
	 		, 'Form'
	 		// ,'Basic'
 		);
                
        //Configure AuthComponent
        $this->Auth->authorize = 'Actions';
        $this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'Users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'Home', 'action' => 'index');
        
         // don't require logging in for login and logout actions
        $this->Auth->allow('lang','display');
        // $this->Auth->allow('*');
        $this->set("utente",$this->Session->read('Auth.User'));
		
		$this->_setLanguage();
		
    }
	
	public function lang($locale) {
		$this->Session->write('Config.language', $locale);
		$this->redirect($this->referer());
	}
	
	function _setLanguage() {
	    if ($this->Session->check('Config.language')) {
			Configure::write('Config.language', $this->Session->read("Config.language"));
	    }
	}
	
}
